package com.rms.hkpc.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.rms.hkpc.R;
import com.rms.hkpc.activity.base.BaseFragmentActivity;
import com.rms.hkpc.adapter.ImagePreviewAdapter;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.control.ViewPager;
import com.rms.hkpc.db.dto.WorkOrderPhotosDto;
import com.rms.hkpc.util.WindowUtil;

import java.util.List;

public class ImagePreviewActivity extends BaseFragmentActivity {

    private ViewPager vpImage;
    private LinearLayout llDot;
    private List<WorkOrderPhotosDto> imageList;
    private int mCurrentPosition;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image_preview;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        WindowUtil.getInstance().setBarBgColor(this, R.color.color_4a);
        fd(R.id.ivClose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        vpImage = fd(R.id.vpImage);
        llDot = fd(R.id.llDot);
        getIntentData();
        setView();
    }

    private void setView() {
        if (imageList == null) {
            return;
        }
        llDot.setVisibility(imageList.size() == 1?View.GONE:View.VISIBLE);
        ImagePreviewAdapter adapter = new ImagePreviewAdapter(this,this,imageList,R.layout.image_preview);
        vpImage.setAdapter(adapter);
        vpImage.setCurrentItem(mCurrentPosition);

        View view;
        for (WorkOrderPhotosDto ignored:imageList){
            view = new View(this);
            view.setBackgroundResource(R.drawable.indicator);
            view.setEnabled(false);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(10,10);
            lp.leftMargin = 20;
            llDot.addView(view,lp);
        }
        llDot.getChildAt(mCurrentPosition).setEnabled(true);
        vpImage.addOnPageChangeListener(new androidx.viewpager.widget.ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                hideAllIndicator();
                llDot.getChildAt(position).setEnabled(true);
                mCurrentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        vpImage.setPageTransformer(false, new androidx.viewpager.widget.ViewPager.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                float normalizedPosition = Math.abs(Math.abs(position) - 1);
                page.setScaleX(normalizedPosition /2 +0.5f);
                page.setScaleY(normalizedPosition /2 +0.5f);
            }
        });
    }

    private void hideAllIndicator() {
        for (int i = 0;i < llDot.getChildCount();i ++){
            llDot.getChildAt(i).setEnabled(false);
        }
    }

    private void getIntentData() {
        if (getIntent() != null) {
            Intent intent = getIntent();
            imageList = (List<WorkOrderPhotosDto>) intent.getSerializableExtra(AllConstants.WHAT);
            mCurrentPosition = intent.getIntExtra(AllConstants.ID,0);
        }
    }

    @Override
    protected void initFragment() {
    }
}